#include "StdAfx.h"
#include "CameraDevice.h"
#include "RobotInstance.h"

namespace RobotPlant { namespace RobotProxy{

	CameraDevice::CameraDevice(RobotInstance* pParent,joint::spawn_type e):DeviceBase(pParent,e)
	{
		_deviceType = DeviceTypeEnum::Camera;
		_deviceIndex = 1;
		chord(GetImageSize, &CameraDevice::Handler_GetImageSize);
		chord(GetViewAngle, &CameraDevice::Handler_GetViewAngle);
		chord(GetQuality, &CameraDevice::Handler_GetQuality);
		chord(GetLastFrame, &CameraDevice::Handler_GetLastFrame);
		chord(GetLastTimeStamp, &CameraDevice::Handler_GetLastTimeStamp);
	}

	CameraDevice::~CameraDevice()
	{
	}

	std::vector<int> CameraDevice::Handler_GetImageSize(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		std::vector<int> value = std::vector<int>();
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		struct ns1__Camera_GetImageSizeResponse temp;
		if (robDeviceInterface.ns1__Camera_GetImageSize(this->_deviceIndex,&temp) != SOAP_OK)
		{
			value.clear();
		}
		else
		{
			value.push_back(temp.Data[0]);
			value.push_back(temp.Data[1]);
		}

		return value;
	}

	double CameraDevice::Handler_GetViewAngle(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		double value = -1;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Camera_GetViewAngle(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	double CameraDevice::Handler_GetQuality(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		double value = -1;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Camera_GetQuality(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	BMP CameraDevice::Handler_GetLastFrame(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		BMP value;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		struct ns1__Camera_GetLastFrameResponse temp; 
		if (robDeviceInterface.ns1__Camera_GetLastFrame(this->_deviceIndex,&temp) != SOAP_OK)
		{
			value.Data = 0;
			value.Size = 0;
		}
		else
		{
			value.Size = temp.Data.ImageData.__size;
			value.Data = new unsigned char[value.Size];
			memcpy(value.Data, temp.Data.ImageData.__ptr, value.Size);
		}

		return value;
	}

	time_t CameraDevice::Handler_GetLastTimeStamp(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		time_t value = -1;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Camera_GetLastTimeStamp(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}
}}